﻿#pragma execution_character_set("utf-8")
#include "Widget.h"
#include "ui_widget.h"
#include "CashContext.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    initUIControl();
}

void Widget::on_okBtn_clicked()
{
    QString txtPrice = ui->priceEdit->text();
    QString txtNum = ui->numEdit->text();
    double totalPrice = 0;

    CashContext* cc = new CashContext(ui->calcCombox->currentIndex());
    totalPrice = cc->GetResult(txtPrice.toDouble()*txtNum.toDouble());
    delete cc;

    m_total += totalPrice;
    QString calcText = ui->calcCombox->currentText();
    ui->listWidget->addItem(QString("单价:%1  数量:%2  %3  合计:%4").arg(txtPrice).arg(txtNum).arg(calcText).arg(totalPrice));
    ui->totalPriceLabel->setText(QString::number(m_total));
}

void Widget::on_resetBtn_clicked()
{
    ui->priceEdit->clear();
    ui->numEdit->clear();
    ui->listWidget->clear();
    ui->totalPriceLabel->setText("0.00");
    m_total = 0;
}

void Widget::initUIControl()
{
    initValidator();
    QStringList sl;
    sl << "正常收费" << "打8折" << "满300减100";
    ui->calcCombox->addItems(sl);
}

void Widget::initValidator()
{
    ui->priceEdit->setValidator(new QDoubleValidator(0, 10000, 2, this));
    ui->numEdit->setValidator(new QIntValidator(0, 100, this));
}

Widget::~Widget()
{
    delete ui;
}
